<?php

class Course_Permission_Model extends CI_Model {

    function getCourseByMemberID($memberID) {
        $this->db->from('COURSE_PERMISSION');
        $this->db->where('MEMBER_ID =', $memberID);
        $results = $this->db->get()->result();

        return $results;
    }

    function getAllCourseByMemberID($memberID) {
        $this->db->select('COURSE.ID, COURSE.CODE, COURSE.NAME, COURSE.HOURS, COURSE.PRICE, COURSE.DETAIL, COURSE.VIDEOS, COURSE_PERMISSION.TIMES');
        $this->db->from('COURSE_PERMISSION');
        $this->db->join('COURSE', 'COURSE_PERMISSION.COURSE_ID = COURSE.ID');
        $this->db->where('COURSE_PERMISSION.MEMBER_ID =', $memberID);

        $query = $this->db->get()->result();

        return $query;
    }

    function getAllCourseByUsername($username) {
        $this->db->select('C.ID, C.CODE, C.NAME');
        $this->db->from('COURSE_PERMISSION AS CP');
        $this->db->join('COURSE AS C', 'CP.COURSE_ID = C.ID', 'INNER');
        $this->db->join('MEMBER AS M', 'CP.MEMBER_ID = M.ID', 'INNER');
        $this->db->where('M.USERNAME =', $username);

        $query = $this->db->get();

        return $query;
    }

    function getCourseHaveOrNot($memberId, $courseId) {
        $this->db->from('COURSE_PERMISSION');
        $this->db->where('MEMBER_ID =', $memberId);
        $this->db->where('COURSE_ID =', $courseId);

        $query = $this->db->get();

        if ($query->num_rows() > 0) {
            return true;
        } else {
            return false;
        }
    }

    function getAllCourseActiveByMemberID($memberID) {
        $this->db->select('COURSE.ID, COURSE.CODE, COURSE.NAME, COURSE.HOURS, COURSE.PRICE, COURSE.DETAIL, COURSE.VIDEOS, COURSE_PERMISSION.TIMES');
        $this->db->from('COURSE_PERMISSION');
        $this->db->join('COURSE', 'COURSE_PERMISSION.COURSE_ID = COURSE.ID');
        $this->db->where('COURSE_PERMISSION.ACTIVE =', false);
        $this->db->where('COURSE_PERMISSION.MEMBER_ID =', $memberID);

        $query = $this->db->get();

        return $query;
    }

    function getRecordByKey($courseID, $memberID) {
        $this->db->from('COURSE_PERMISSION');
        $this->db->where('COURSE_ID', $courseID);
        $this->db->where('MEMBER_ID', $memberID);
        $results = $this->db->get()->row();

        return $results;
    }

    function updatePermissionTimes($times, $memberID, $courseID) {
//            echo 'times = '.$times;
//            echo '$memberID = '.$memberID;
//            echo '$courseID = '.$courseID;
        $data = array(
            'TIMES' => $times
        );

        $this->db->where('MEMBER_ID', $memberID);
        $this->db->where('COURSE_ID', $courseID);
        $this->db->update('COURSE_PERMISSION', $data);
    }

    function getDDLCourseByMemberID($memberID) {
        $this->db->select('COURSE.ID, COURSE.NAME');
        $this->db->from('COURSE_PERMISSION');
        $this->db->join('COURSE', 'COURSE_PERMISSION.COURSE_ID = COURSE.ID');
        $this->db->where('COURSE_PERMISSION.MEMBER_ID =', $memberID);
        $result = $this->db->get();
        $return = array();
        if ($result->num_rows() > 0) {
            foreach ($result->result_array() as $row) {
                $return[$row['ID']] = $row['NAME'];
            }
        }

        return $return;
    }

    function countPermissionByMemberID($memberID) {
        $this->db->from('COURSE_PERMISSION');
        $this->db->where('MEMBER_ID', $memberID);
        $this->db->count_all_results();
    }

    function getAllCourse($per_page, $offset) {
        $this->db->select('COURSE.CODE, COURSE.NAME, COURSE.HOURS, COURSE.PRICE, COURSE.DETAIL, COURSE.VIDEOS , MEMBER.USERNAME');
        $this->db->from('COURSE_PERMISSION');
        $this->db->join('COURSE', 'COURSE_PERMISSION.COURSE_ID = COURSE.ID');
        $this->db->join('MEMBER', 'COURSE_PERMISSION.MEMBER_ID = MEMBER.ID');
        $this->db->limit($per_page, $offset);
        $query = $this->db->get();

        return $query;
    }

    function searchCourse($search_user, $courseCode, $courseName, $per_page, $offset) {
        if (!empty($per_page) && !empty($offset)) {
            $this->db->select('M.USERNAME, C.ID, C.CODE, C.NAME, C.HOURS, C.PRICE, C.DETAIL, C.VIDEOS, CP.TIMES, CP.ACTIVE, CP.MEMBER_ID');
            $this->db->from('COURSE_PERMISSION AS CP');
            $this->db->join('COURSE AS C', 'CP.COURSE_ID = C.ID', 'INNER');
            $this->db->join('MEMBER AS M', 'CP.MEMBER_ID = M.ID', 'INNER');
            $this->db->where('CP.ACTIVE', false);
            $this->db->where('M.USERNAME', $search_user);
            $this->db->limit($per_page, $offset);
            $query = $this->db->get();
        } else {
            $this->db->select('M.USERNAME, C.ID, C.CODE, C.NAME, C.HOURS, C.PRICE, C.DETAIL, C.VIDEOS, CP.TIMES, CP.ACTIVE, CP.MEMBER_ID');
            $this->db->from('COURSE_PERMISSION AS CP');
            $this->db->join('COURSE AS C', 'CP.COURSE_ID = C.ID', 'INNER');
            $this->db->join('MEMBER AS M', 'CP.MEMBER_ID = M.ID', 'INNER');
            if ($search_user) {
                $this->db->where('M.USERNAME', $search_user);
            }
            $this->db->where('CP.ACTIVE', false);
            $query = $this->db->get();
        }
        return $query;
    }

//        function getAllCourseNotActive(){
//            $this->db->select('M.USERNAME, C.CODE, C.NAME, C.HOURS, C.PRICE, C.DETAIL, C.VIDEOS, CP.TIMES');
//            $this->db->from('COURSE_PERMISSION AS CP');
//            $this->db->join('COURSE AS C', 'CP.COURSE_ID = C.ID', 'INNER');
//            $this->db->join('MEMBER AS M', 'CP.MEMBER_ID = M.ID', 'INNER');
//            $this->db->where('CP.ACTIVE', false);
//
//            $query = $this->db->get();
//
//            return $query;
//        }

    function getAllCourseNotActive($per_page = null, $offset = null) {
        if (!empty($per_page) && !empty($offset)) {
            $this->db->select('M.USERNAME, C.ID, C.CODE, C.NAME, C.HOURS, C.PRICE, C.DETAIL, C.VIDEOS, CP.TIMES, CP.ACTIVE, CP.MEMBER_ID');
            $this->db->from('COURSE_PERMISSION AS CP');
            $this->db->join('COURSE AS C', 'CP.COURSE_ID = C.ID', 'INNER');
            $this->db->join('MEMBER AS M', 'CP.MEMBER_ID = M.ID', 'INNER');
            $this->db->where('CP.ACTIVE', false);
            $this->db->limit($per_page, $offset);
            $query = $this->db->get();
        } else {
            $this->db->select('M.USERNAME, C.ID, C.CODE, C.NAME, C.HOURS, C.PRICE, C.DETAIL, C.VIDEOS, CP.TIMES, CP.ACTIVE, CP.MEMBER_ID');
            $this->db->from('COURSE_PERMISSION AS CP');
            $this->db->join('COURSE AS C', 'CP.COURSE_ID = C.ID', 'INNER');
            $this->db->join('MEMBER AS M', 'CP.MEMBER_ID = M.ID', 'INNER');
            $this->db->where('CP.ACTIVE', false);
            $query = $this->db->get();
        }
        return $query;
    }

    function approveCourse($courseID, $memberID) {
        $this->db->trans_start();
        $data = array(
            'ACTIVE' => true
        );

        $this->db->where('MEMBER_ID', $memberID);
        $this->db->where('COURSE_ID', $courseID);
        $this->db->update('COURSE_PERMISSION', $data);

        $this->load->Model('Menu_Permission_Model');
        $result = $this->Menu_Permission_Model->isWatchVideoEmpty($memberID);

        if ($result) {
            //insert menu permission 6 = Watch Video
            $dataMenu = array(6);
            foreach ($dataMenu as $menu) {

                $data = array(
                    'MEMBER_ID' => $memberID,
                    'MENU_ID' => $menu);

                $this->db->insert('menu_permission', $data);
            }
        }

        if ($this->db->trans_status() === FALSE) {
            $this->db->trans_rollback();
        } else {
            $this->db->trans_commit();
        }
    }

}

?>
